﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using Bordspil.Models;
using System.Net;
using System.Net.Mail;
using System.Configuration;

namespace Bordspil.Controllers
{
    public class HomeController : Controller
    {
        private IAppRepository m_repo = null;

        public HomeController()
        {
            m_repo = new AppRepository();
        }

        public ActionResult Index()
        {
            return View();
        }

        [HttpPost]
        public ActionResult LogIn(Bordspil.Models.User user)
        {
            if(ModelState.IsValid)
            {
                if (IsValid(user.username, user.password))
                {
                    FormsAuthentication.SetAuthCookie(user.username, false);
                    return RedirectToAction("Index", "User");
                }
                else
                {
                    ModelState.AddModelError("", "Skráðiru inn réttar upplýsingar?");
                }
            }
            return View("ErrorPageForLoginRegister");
        }

        [HttpPost]
        public ActionResult Register(Bordspil.Models.User user)
        {
            if(ModelState.IsValid)
            {
                m_repo.AddUser(user);
                return RedirectToAction("index");
            }
            else
            {
                ModelState.AddModelError("", "Eitthvað fór úrskeiðis prófaðu aftur");
            }
            return View("Index");
        }

        public ActionResult LogOut()
        {
            FormsAuthentication.SignOut();
            return RedirectToAction("Index", "Home");
        }

        private bool IsValid(string username, string password)
        {
            var crypto = new SimpleCrypto.PBKDF2();
            bool isValid = false;
            var user = m_repo.GetAllUsers().FirstOrDefault(x => x.username == username);

            if (user != null)
            {
                if (user.password == crypto.Compute(password, user.passwordSalt))
                {
                    isValid = true;
                }       
            }
            return isValid;
        }

        [HttpGet]
        public ActionResult ForgotPassword()
        {
            return View(); 
        }

        [HttpPost]
        public ActionResult ForgotPassword(Bordspil.Models.User user)
        {         
            using (MailMessage message = new MailMessage())
            {                 
                message.To.Add(user.email);
                message.Subject = "Email subject line";
                message.Body = "Hello world!";

                using (SmtpClient client = new SmtpClient())
                {
                    client.EnableSsl = true; 
                    client.Send(message);
                }
            }       
            return View();
        }
    }
}

        
